//
//  TrainingGoalSettingCell.swift
//  DaRings
//
//  Created by HFY on 2022/11/30.
//

import UIKit
import MYModule

open class TrainingGoalSettingCell: CollectionViewCell {
    
    public lazy var layout: UICollectionViewFlowLayout = {
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        
        return layout
    }()
    
    public lazy var collectionView: CollectionView = {
        
        let collectionView = CollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.isScrollEnabled = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()
    
    
    open var model: TrainingGoalModel? {
        
        didSet {
            
            collectionView.reloadData()
        }
    }
    
    open override func makeUI() {
        super.makeUI()
        
        
        collectionView.register(TrainingGoalItemCell.self, forCellWithReuseIdentifier: TrainingGoalItemCell.reuseIdentifier)
        collectionView.register(TrainingGoalSelectedCell.self, forCellWithReuseIdentifier: TrainingGoalSelectedCell.reuseIdentifier)
        
       
        collectionView.dataSource = self
        collectionView.delegate = self
        
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
    }
    
}

extension TrainingGoalSettingCell: UICollectionViewDataSource, UICollectionViewDelegate {
    
    public func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return 2
    }
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if section == 0 {
            return 1
        }
        
        return model?.items.count ?? 0
        
    }
    
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if indexPath.section == 0 {
        
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TrainingGoalSelectedCell.reuseIdentifier, for: indexPath) as! TrainingGoalSelectedCell
            
            cell.contentType = model?.contentType
            cell.selectedItem = model?.selectedItem
            return cell
        }
        
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TrainingGoalItemCell.reuseIdentifier, for: indexPath) as! TrainingGoalItemCell
        
        cell.model = model?.items[indexPath.item]
        
        return cell

        
    }
    
    
    
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        guard indexPath.section != 0  else { return }

        guard let item = model?.items[indexPath.item] else { return }
        model?.selectedItem?.isSelected = false
        item.isSelected = true
        
        collectionView.reloadData()
    }
}

extension TrainingGoalSettingCell: UICollectionViewDelegateFlowLayout {
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        if indexPath.section == 0 {
            
            return CGSize(width: UIScreen.main.bounds.size.width, height: 220)
        }
        
        return CGSize(width: 105, height: 60)
        
    }
    
}
